package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.fragment.SmartAppliancesFragment;
import com.qiantu.phone.utils.DataHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * time   : 2021/12/22
 */
public final class MultipleSelectDeviceActivity extends AppActivity {

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, MultipleSelectDeviceActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private SmartAppliancesFragment mSmartAppliancesFragment;
    private String deviceMainType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_multiple_select_device;
    }

    @Override
    protected void initView() {
        deviceMainType = getString("deviceMainType");
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if(!TextUtils.isEmpty(deviceMainType)){
            mSmartAppliancesFragment = SmartAppliancesFragment.newInstance(SmartAppliancesFragment.SearchType.SEARCH_TYPE_DEVICETYPE,deviceMainType);
        }else{
            mSmartAppliancesFragment = SmartAppliancesFragment.newInstance(SmartAppliancesFragment.SearchType.SEARCH_TYPE_ALL,deviceMainType);
        }
        mSmartAppliancesFragment.setShowOffline(false);
        mSmartAppliancesFragment.setSelectable(true);
        fragmentTransaction.add(R.id.content_view, mSmartAppliancesFragment);
        fragmentTransaction.commitNowAllowingStateLoss();
    }

    @Override
    protected void initData() {
        List<DeviceBean> selectedDevices = (List<DeviceBean>) DataHolder.getInstance().getData(deviceMainType);
        if(selectedDevices != null && selectedDevices.size() > 0){
            mSmartAppliancesFragment.checkSelected(selectedDevices);
            getTitleBar().getRightView().setEnabled(true);
        }else{
            getTitleBar().getRightView().setEnabled(false);
        }
    }

    public void onSelectedDevices(ArrayList<DeviceBean> selectedDevices){
        if(selectedDevices == null || selectedDevices.size() == 0){
            getTitleBar().getRightView().setEnabled(false);
        }else{
            getTitleBar().getRightView().setEnabled(true);
        }
    }

    @Override
    public void onRightClick(View view) {
        List<DeviceBean> selectedDevices = mSmartAppliancesFragment.getSelectedDevices();
        if(selectedDevices != null){
            DataHolder.getInstance().setData(deviceMainType, selectedDevices);
            setResult(RESULT_OK);
            finish();
        }else{
            toast("保存失败");
        }
    }
}